package spring.cloud.tasks.common.zookeeper;


import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooKeeper;
import spring.cloud.tasks.client.Worker;
import spring.cloud.tasks.client.WorkerInfo;
import spring.cloud.tasks.client.batch_task_processor.TaskWorkerInstanceServerInfo;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * tbschedule回归版 8.15 集中式管理 任务调度执行框架
 */
@Slf4j
public class ZooKeeperBatchTaskDataManager extends ZooKeeperDataManager {
    @Setter
    @Getter
    private ZooKeeperManager zooKeeperManager;
    @Setter
    @Getter
    private long zooKeeperBaseDateTimeLong = 0;
    @Setter
    @Getter
    private long localBaseDateTimeLong = 0;
    @Setter
    @Getter
    private String taskRootPath;
    @Setter
    @Getter
    private String workerIdsRootPath;

    public ZooKeeperBatchTaskDataManager(ZooKeeperManager zooKeeperManager) throws Exception {
        this.zooKeeperManager = zooKeeperManager;
        //
        this.localBaseDateTimeLong = System.currentTimeMillis();
        String systemTimePath = this.zooKeeperManager.getZooKeeper().create(this.zooKeeperManager.getRootPath() + ZookeeperNodeConstants.$system_time, null, this.zooKeeperManager.getAclList(), CreateMode.EPHEMERAL_SEQUENTIAL);
        this.zooKeeperBaseDateTimeLong = this.zooKeeperManager.getZooKeeper().exists(systemTimePath, false).getCtime();
        ZooKeepers.directoryStructureDelete(getZooKeeper(), systemTimePath);
        if (Math.abs(this.zooKeeperBaseDateTimeLong - this.localBaseDateTimeLong) > 5000) {
            log.error("Zookeeper " + Math.abs(this.zooKeeperBaseDateTimeLong - this.localBaseDateTimeLong) + " ms");
        }
        //
        this.taskRootPath = this.zooKeeperManager.getRootPath() + ZookeeperNodeConstants.BatchTask.$task;
        if (this.getZooKeeper().exists(this.taskRootPath, false) == null) {
            ZooKeepers.createPath(getZooKeeper(), this.taskRootPath, this.zooKeeperManager.getAclList(), CreateMode.PERSISTENT);
        }
        this.workerIdsRootPath = this.zooKeeperManager.getRootPath() + ZookeeperNodeConstants.BatchTask.$workerIds;
        if (this.getZooKeeper().exists(this.workerIdsRootPath, false) == null) {
            ZooKeepers.createPath(getZooKeeper(), this.workerIdsRootPath, this.zooKeeperManager.getAclList(), CreateMode.PERSISTENT);
        }
    }

    public ZooKeeper getZooKeeper() throws Exception {
        return this.zooKeeperManager.getZooKeeper();
    }

    public long zooKeeperCurrentTimeMillis() {
        return (this.zooKeeperBaseDateTimeLong - this.localBaseDateTimeLong) + System.currentTimeMillis();//求的是zk时间
    }

    public String getTaskPath(String taskId) {
        return this.taskRootPath + ZookeeperNodeConstants.slash + taskId;
    }


    public String getProcessorsPath(String taskId) {
        return getTaskPath(taskId) + ZookeeperNodeConstants.slash + ZookeeperNodeConstants.BatchTask.processorIds;
    }

    public String getProcessorPath(String taskId, String processorId) {
        return getProcessorsPath(taskId) + ZookeeperNodeConstants.slash + processorId;
    }

    public String getWorkerPath(String workerId) {
        return this.workerIdsRootPath + ZookeeperNodeConstants.slash + workerId;
    }

    public String getTaskWorkersPath(String taskId) {
        return this.taskRootPath + ZookeeperNodeConstants.slash + taskId + ZookeeperNodeConstants.slash + ZookeeperNodeConstants.BatchTask.workerIds;
    }

    public String getTaskWorkerPath(String taskId, String workerId) {
        return getTaskWorkersPath(taskId) + ZookeeperNodeConstants.slash + workerId;
    }


    //
    public Map<String, TaskWorkerInstanceServerInfo> loadWorkerTaskBatchInfoListByWorkerId(String workerId) {
        return new HashMap<String, TaskWorkerInstanceServerInfo>();
    }

    @Override
    public List<String> registerWorkerToPermittedTaskList(Worker worker) {
        return null;
    }

    @Override
    public List<String> registerWorkerToPermittedTaskListWhenPongPong(Worker worker) {
        return null;
    }

    @Override
    public WorkerInfo loadWorkerInfo(String workerId) throws FileNotFoundException {
        return null;
    }

    @Override
    public void unRegisterWorker(Worker worker) throws Exception {

    }

    public List<String> getTaskInstanceIdList(String taskId, String workerId) {
        return null;
    }

    public TaskWorkerInstanceServerInfo getWorkerTaskInstanceBatchInfo(String taskId, String taskInstanceId) {
        return null;
    }
}



 